<template>
  <el-dialog :visible="showDialog" title="新增员工" @close="close">
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="员工姓名" prop="username">
        <el-input v-model="formData.username" style="width: 80%" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width: 80%" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" style="width: 80%" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width: 80%" value="">
          <el-option
            v-for="item in EmployeeEnum.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width: 80%" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" style="width: 80%" @focus="getDeparts" />
        <el-tree
          v-if="showTree"
          v-loading="loading"
          default-expand-all
          class="tree-box"
          :props="defaultProps"
          :data="treeData"
          @node-click="selectNode"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" style="width: 80%" />
      </el-form-item>
    </el-form>

    <template #footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-row type="flex" justify="space-between">
            <el-button size="mini" @click="close">取消</el-button>
            <el-button type="primary" size="mini" @click="btnOk">确定</el-button>
          </el-row>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getDepartments } from '@/api/user'
import { tranListToTreeData } from '@/utils'
import EmployeeEnum from '@/api/constant/employees'
import { addEmployee } from '@/api/employees'

export default {
  name: 'AddEmployee',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      EmployeeEnum,
      showTree: false,
      loading: false,
      treeData: [],
      defaultProps: {
        label: 'name'
      },
      formData: {
        username: '', // 用户姓名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门名称
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [{ required: true, message: '用户姓名不能为空', trigger: 'blur' }, {
          min: 1, max: 4, message: '用户姓名为1-4位'
        }],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }, {
          pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'
        }],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }]
      }
    }
  },
  methods: {
    selectNode(node) {
      this.formData.departmentName = node.name
      this.showTree = false
    },
    async getDeparts() {
      this.showTree = true
      this.loading = true
      const { depts = [] } = await getDepartments()
      this.treeData = tranListToTreeData(depts, '')
      this.loading = false
    },
    async btnOk() {
      await this.$refs.formRef.validate()
      await addEmployee(this.formData)
      this.$message.success('添加成功')
      this.$parent.getList()
      this.close()
    },
    close() {
      // 重置数据
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      this.$parent.showDialog = false
      this.$refs.formRef.resetFields()
    }
  }
}
</script>

<style scoped>
.tree-box {
  position: absolute;
  z-index: 1;
  width: 80%;
  border: 1px solid blue;
  border-radius: 5px;
  overflow-y: auto;
  height: 300px;
}
</style>
